<?php
namespace Home\Model;
use Think\Model;
class CommentModel extends Model {
    public function addComment($data){
        $this->add($data);
    }

//    public function deleteAllComment(){
//        return $this->delete();
//    }

    public function deleteCommentByUserId($user_id){
        $this->where("from_id='$user_id' or to_id='$user_id' ")->delete();
    }

    public function deleteCommentByArticleId($article_id){
        $this->where("article_id='$article_id'")->delete();
    }

    public function deleteCommentById($id){
        D('Notice')->deleteNoticeByCommentId($id);
        $this->where("id='$id'")->delete();
    }



    public function updateCommentById($id,$data){
        $this->where("id='$id'")->save($data);
    }


    public function getAllComment(){
        return $this->select();
    }
    public function getCommentById($id){
//        dump( $this->where("id='$id'")->buildSql());
        return $this->where("id='$id'")->select();
    }

    public function getAllCommentById($id){
        return $this->where("id='$id'")->select();
    }

    public function getAllCommentByToId($to_id){
        return $this->where("to_id='$to_id'")->select();
    }

    public function getUnViewedCommentByToId($to_id){
        return $this->where("to_id='$to_id' and view_status='false'" )->select();
    }
    public function getViewedCommentByToId($to_id){
        return $this->where("to_id='$to_id' and view_status='true'" )->select();
    }

    public function getCommentByFromIdDesc($from_id){
        return $this->where("from_id='$from_id'")->order('id desc')->select();
    }

    public function getRecentCommentByFromIdDesc($from_id,$limit_number=5){
        $comments = $this->where("from_id='$from_id'")->limit($limit_number)->order('id desc')->select();
        foreach($comments as $key => $comment){
            $article=D('Article')->getArticleByArticleId($comment['article_id']);
            $comments[$key]['title']=$article[0]['title'];
            $to_user=D('User')->getUserById($comment['to_id']);
            $comments[$key]['to_name']=$to_user[0]['username'];
        }
        return $comments;
    }

    public function getAllCommentsByToId($to_id){
//        $this->$this->where("from_id='$from_id'")->limit($limit_number)->order('id desc')->select();
        return ;
    }


    public function getDistinctCommentByNoticeToId($to_id){
        $notices = D('notice')->getComment_NoticeByToId($to_id);
        $comments=[];
        foreach($notices as $key => $notice){
            $from_id=$notice['from_id'];
            //判断是否已在通知队列中 同一个对象发过来的
            if($comments[$from_id]['to_id']==$to_id){
            }else{
                $comments[$from_id]['to_id']=$to_id;
            }
            //判断是否为未提醒数据
            if($notice['view_status']=='false'){
                $comments[$from_id]['notice_count']++;
                $comment_content=D('Comment')->getAllCommentById($notice['notice_id']);
                $comments[$from_id]['last_comment']=$comment_content[0]['content'];
                $comments[$from_id]['create_time']=$comment_content[0]['create_time'];
                $comments[$from_id]['notice_id']=$notices[0]['notice_id'];
            }
//            dump($notices); dump($comments);die();
        }
        foreach($comments as $key=>$comment){
            $from_user=D('User')->getUserById($key);
            $comments[$key]['from_name']=$from_user[0]['username'];
            $avatar_data=D('Avatar')->getAvatarById($key);
            $comments[$key]['avatar_name']=$avatar_data[0]['name'];
            $article=D('Article')->getArticleById($key);
            $comments[$key]['article_name']=$article[0]['title'];
        }
        return $comments;
    }

    public function getAllCommentsByArticleId($article_id){
        $Comments=$this->where("article_id='$article_id'")->select();
        foreach($Comments as $key => $Comment){

            $from_name=D('User')->getUserById($Comment['from_id']);
            $Comments[$key]['from_name']=$from_name[0]['username'];

            $Avatar_data=D('Avatar')->getAvatarByUserId($Comment['from_id']);
            $Comments[$key]['avatar_name']=$Avatar_data[0]['name'];

            $article_author=D('User')->getUserByArticleId($article_id);

            $Comments[$key]['article_author']=$article_author[0]['username'];

            $to_user=D('User')->getUserById($Comment['to_id']);
            if($Comment['to_type']=='user'){
                $Comments[$key]['to_id']=$to_user[0]['id'];
                $Comments[$key]['to_name']=$to_user[0]['username'];
            }

        }
        return $Comments;
    }


    public function getCommentByArticleId($article_id){
        return $this->where("article_id='$article_id'")->select();
    }

    public function getCommentByArticleIdDesc($article_id){
        return $this->where("article_id='$article_id'")->order('id desc')->select();
    }

    public function getCommentByName($name){
        return $this->where("name='$name'")->select();
    }


}